package com.ysy.logistics.service.system.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.common.myenum.StatusEnum;
import com.ysy.logistics.entities.system.SysNotice;
import com.ysy.logistics.entities.system.SysUser;
import com.ysy.logistics.entities.system.UserNotice;
import com.ysy.logistics.mapper.system.SysNoticeMapper;
import com.ysy.logistics.mapper.system.SysUserMapper;
import com.ysy.logistics.mapper.system.UserNoticeMapper;
import com.ysy.logistics.myenum.NoticeType;
import com.ysy.logistics.service.system.SysNoticeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 姚斯羽
 * @date 2025/4/18 11:31
 * @description:
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {
    @Resource
    private SysNoticeMapper sysNoticeMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private UserNoticeMapper userNotice;

    @Override
    public boolean saveNoticeStock(String title,String content) {
        //构建信息
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType(NoticeType.NOTICE_TYPE_STOCK.getCode());
        sysNotice.setTitle(title);
        sysNotice.setContent(content);
        sysNotice.setPublishTime(new Date());
        //保存
        if (sysNoticeMapper.insert(sysNotice)<1){
            return false;
        }
        //向用户发送信息
        //根据通知类型查询用户
        List<SysUser> userByNoticeType = sysUserMapper.getUserByNoticeType(NoticeType.NOTICE_TYPE_STOCK.getCode());
        if (CollectionUtil.isNotEmpty(userByNoticeType)){
            List<UserNotice> userNotices = userByNoticeType.stream().map(user -> {
                UserNotice userNotice = new UserNotice();
                userNotice.setUserId(user.getId())
                        .setNoticeId(sysNotice.getId())
                        .setState(false);
                userNotice.setStatus(StatusEnum.ENABLE.getCode());
                userNotice.setCreateTime(new Date());
                return userNotice;
            }).collect(Collectors.toList());
            userNotice.insert(userNotices);
        }
        return true;
    }

    @Override
    public boolean saveNoticeDevice(String title, String content) {
        //构建信息
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType(NoticeType.DEVICE_NOTICE.getCode());
        sysNotice.setTitle(title);
        sysNotice.setContent(content);
        sysNotice.setPublishTime(new Date());
        //保存
        if (sysNoticeMapper.insert(sysNotice)<1){
            return false;
        }
        //向用户发送信息
        //根据通知类型查询用户
        List<SysUser> userByNoticeType = sysUserMapper.getUserByNoticeType(NoticeType.DEVICE_NOTICE.getCode());
        if (CollectionUtil.isNotEmpty(userByNoticeType)){
            List<UserNotice> userNotices = userByNoticeType.stream().map(user -> {
                UserNotice userNotice = new UserNotice();
                userNotice.setUserId(user.getId())
                        .setNoticeId(sysNotice.getId())
                        .setState(false);
                userNotice.setStatus(StatusEnum.ENABLE.getCode());
                userNotice.setCreateTime(new Date());
                return userNotice;
            }).collect(Collectors.toList());
            userNotice.insert(userNotices);
        }
        return true;
    }
}
